<?php get_header(); ?>
	
		<div id="container_result">	
			<div id="content">
<?php
//define field id
$fieldcity = 1;
$fieldcountry = 4;
$fieldpermonth = 16;
$fieldbedroom = 8;
$fieldtoilets = 7;
$fieldfloors = 9;
$fieldgarden = 28;
$fieldgarage = 27;
$fielddescription = 12;
$fieldimage1 = 26;

//get all parameter
$bedroomrq = get_query_var('bedroompara');
$countryrq = get_query_var('countrypara');
$countryrq = str_replace('-',' ',$countryrq);
$cityrq = get_query_var('citypara');
$cityrq = str_replace('-',' ',$cityrq);

//var_dump($countryrq);exit;
//$cityrq = $_REQUEST['citypara'];
$minpricerq = get_query_var('minpricepara');
$maxpricerq = get_query_var('maxpricepara');
//var_dump($bedroomrq);exit;
if ($bedroomrq == '' || $bedroomrq == 'all'){
	$bedroomrq = 'all';
}

if ($maxpricerq == '' || $maxpricerq == 'NoMax'){
	$maxpricerq = null;
}
if ($minpricerq == '' || $minpricerq == 'NoMin'){
	$minpricerq = null;
}

if (!$countryrq){
	$countryrq = 'United Kingdom';
}
if (!$cityrq){
	$cityrq = 'All Cities';
}
echo $countryrq.'--'.$cityrq.'--'.$minpricerq.'--'.$maxpricerq.'--'.$bedroomrq.'<br/>';
//filter by country
$searchresults = $wpdb->get_results("SELECT lead_id from wp_rg_lead_detail where field_number=$fieldcountry and value='$countryrq'");
$idarr = '';
foreach ($searchresults as $perentries){
	$idarr .= $perentries->lead_id.',';
}
$idarr .= '0';
//then filter by city
//for all cities
if ($cityrq == 'All Cities'){
	$searchresults = $wpdb->get_results("SELECT lead_id from wp_rg_lead_detail where lead_id IN ($idarr) and field_number=$fieldcity");

$idarr = '';
foreach ($searchresults as $perentries){
	$idarr .= $perentries->lead_id.',';
}
$idarr .= '0';
}
//for one city
if ($cityrq != 'All Cities'){
	$searchresults = $wpdb->get_results("SELECT lead_id from wp_rg_lead_detail where lead_id IN ($idarr) and field_number=$fieldcity and value='$cityrq'");

$idarr = '';
foreach ($searchresults as $perentries){
	$idarr .= $perentries->lead_id.',';
}
$idarr .= '0';
}

//then filter by min price and max price
if ($minpricerq && $maxpricerq){
	$searchresults = $wpdb->get_results("SELECT lead_id from wp_rg_lead_detail where lead_id IN ($idarr) and field_number=$fieldpermonth and CONVERT(value, SIGNED INTEGER) >= CONVERT('$minpricerq', SIGNED INTEGER) and CONVERT(value, SIGNED INTEGER) <= CONVERT('$maxpricerq', SIGNED INTEGER)");

$idarr = '';
foreach ($searchresults as $perentries){
	$idarr .= $perentries->lead_id.',';
}
$idarr .= '0';
}
//filter by min price only
if ($minpricerq && !$maxpricerq){
	$searchresults = $wpdb->get_results("SELECT lead_id from wp_rg_lead_detail where lead_id IN ($idarr) and field_number=$fieldpermonth and CONVERT(value, SIGNED INTEGER) >= CONVERT('$minpricerq', SIGNED INTEGER)");

$idarr = '';
foreach ($searchresults as $perentries){
	$idarr .= $perentries->lead_id.',';
}
$idarr .= '0';
}
//filter by max price only
if (!$minpricerq && $maxpricerq){
	$searchresults = $wpdb->get_results("SELECT lead_id from wp_rg_lead_detail where lead_id IN ($idarr) and field_number=$fieldpermonth and CONVERT(value, SIGNED INTEGER) <= CONVERT('$maxpricerq', SIGNED INTEGER)");

$idarr = '';
foreach ($searchresults as $perentries){
	$idarr .= $perentries->lead_id.',';
}
$idarr .= '0';
}

//filter by bedroom
if ($bedroomrq != 'all'){
	$searchresults = $wpdb->get_results("SELECT lead_id from wp_rg_lead_detail where lead_id IN ($idarr) and field_number=$fieldbedroom and value='$bedroomrq'");

$idarr = '';
foreach ($searchresults as $perentries){
	$idarr .= $perentries->lead_id.',';
}
$idarr .= '0';
}

?>			
			
<?php
$idarr = substr($idarr,0,-2);

$searchresults = $wpdb->get_results( " SELECT filtered.sort, l.*, d.field_number, d.value, d.lead_id FROM wp_rg_lead l INNER JOIN wp_rg_lead_detail d ON d.lead_id = l.id INNER JOIN ( SELECT @rownum:=@rownum + 1 as sort, id FROM ( SELECT distinct l.id FROM wp_rg_lead l INNER JOIN wp_rg_lead_detail d ON d.lead_id = l.id WHERE l.id IN ($idarr) and l.form_id=2 ORDER BY date_created DESC LIMIT 0,20 ) page ) filtered ON filtered.id = l.id ORDER BY filtered.sort" );
//get the result and display
$fieldcityarr = 'fieldcityarr';
$fieldcountryarr = 'fieldcountryarr';
$fieldpermontharr = 'fieldpermontharr';
$fieldbedroomarr = 'fieldbedroomarr';
$fieldtoiletsarr = 'fieldtoiletsarr';
$fieldfloorsarr = 'fieldfloorsarr';
$fieldgardenarr = 'fieldgardenarr';
$fieldgaragearr = 'fieldgaragearr';
$fielddescriptionarr = 'fielddescriptionarr';
$fieldimage1arr = 'fieldimage1arr';

$listtable = array();
//get fields value and put it to array
if (!$searchresults){
	echo '<div id="noresultfound">Sorry no listings latch your search</div>';
}
else{
	foreach ($searchresults as $perresult){
		if ($perresult->field_number == $fieldcity){
			$listtable[$fieldcityarr][] = $perresult->value;
		}
		if ($perresult->field_number == $fieldcountry){
			$listtable[$fieldcountryarr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldprice){
			$listtable[$fieldpricearr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldpermonth){
			$listtable[$fieldpermontharr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldbedroom){
			$listtable[$fieldbedroomarr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldtoilets){
			$listtable[$fieldtoiletsarr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldfloors){
			$listtable[$fieldfloorsarr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldgarden){
			$listtable[$fieldgardenarr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldgarage){
			$listtable[$fieldgaragearr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fielddescription){
			$listtable[$fielddescriptionarr][] = $perresult->value;
		}	
		if ($perresult->field_number == $fieldimage1){
			$listtable[$fieldimage1arr][] = $perresult->value;
		}
		$listtable[$fieldleadid][] = $perresult->lead_id;
		
	}
	$leadid_arrnosort = array_unique($listtable[$fieldleadid]);
	$leadid_arr = array();
	foreach($leadid_arrnosort as $leadarrperval){
		$leadid_arr[] = $leadarrperval;
	}
	echo '<h2>Listt all entries ,Include Unapprove entries</h2><br/>';
	$tablelistentries = '<table id="searchresultlisttableletyourlet"><tr><th>Country</th><th>City</th><th>Price</th><th>Per Month</th><th>Kitchen</th><th>Toilets</th><th>Parking Space</th><th>Description</th><th>Image1</th></tr>';
	//list fields value from array,and increase indexnumber
	$indexnumber = 0;
	foreach ($listtable[$fieldcityarr] as $fieldscitylist){
		$indexnumberlist = $indexnumber+1;
		/*
		$tablelistentries .= '<tr>';
		$tablelistentries .= '<td>'.$listtable[$fieldcountryarr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldcityarr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldpermontharr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldbedroomarr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldtoiletsarr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldfloorsarr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldgardenarr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldgaragearr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fielddescriptionarr][$indexnumber].'</td>';
		$tablelistentries .= '<td>'.$listtable[$fieldimage1arr][$indexnumber].'</td>';
		$tablelistentries .= '</tr>';
		*/
		$fullurl = get_option('home').'/residential-detail/?entryid='.$leadid_arr[$indexnumber];
		echo '


		
		<div class="search-result">
		<div class="result-title">
			<h2><a href="'.$fullurl.'">'.$listtable[$fieldcityarr][$indexnumber].' | '.$listtable[$fieldcountryarr][$indexnumber].'</a></h2>
		</div>  
		<div class="result-details">
        <div class="result-image">
		<a href="http://www.rent.co.uk/houses-to-let/Galway-Manor-Upper-Newtownards-Road-Dundonald-Belfast-Antrim/11306324">                    
		<img width="190" height="145" src='.$listtable[$fieldimage1arr][$indexnumber].' id='.$indexnumberlist.' alt="Galway Manor, Upper Newtownards Road, Dundonald, Belfast, Antrim" class="sresult_thumb" title="Galway Manor, Upper Newtownards Road, Dundonald, Belfast, Antrim">
		<script type="text/javascript">(function(){var e=document.getElementById("1");e.onerror=function(){e.onerror=null;e.src="http://www.rent.co.uk/images/no_image.gif"}}())</script> </a>
        </div>
        <div class="result_decription">
		<h4>&pound; £'.$listtable[$fieldpermontharr][$indexnumber].' PCM</h4>
            <ul>
            <li>Bedroom:<span>'.$listtable[$fieldbedroomarr][$indexnumber].'</span></li>
            <li>Toilet:<span>'.$listtable[$fieldtoiletsarr][$indexnumber].'</span></li>
            <li>Floors:<span>'.$listtable[$fieldfloorsarr][$indexnumber].'</span></li>
            <li>Garden:<span>'.$listtable[$fieldgardenarr][$indexnumber].'</span></li>
			<li>Garage:<span>'.$listtable[$fieldgaragearr][$indexnumber].'</span></li>      
            </ul> 
			<span class="entry-date">Property added '.$listtable[$fielddescriptionarr][$indexnumber].' days ago</span>
		<div class="more-button"><a href="http://www.rent.co.uk/houses-to-let/Galway-Manor-Upper-Newtownards-Road-Dundonald-Belfast-Antrim/11306324"><span></span></a></div> 
		</div>
		</div>
		</div>';
		
		$indexnumber++;	
	}
}
$tablelistentries .= '</table>';
//echo $tablelistentries;

?>			
			
			</div><!-- #content --> <?php get_sidebar(); ?>			
		</div><!-- #container -->
<script>

		$(document).ready(function() {	

		

		function readPage(){
			var countrypara = document.getElementById('res_country').value;
			var citypara = document.getElementById('res_city').value;
			var minpricepara = document.getElementById('res_minprice').value;
			var maxpricepara = document.getElementById('res_maxprice').value;
			var bedroompara = document.getElementById('res_bedroom').value;
			var rooturl = '<?php echo get_option('home'); ?>';
			while (countrypara.indexOf(" ") > 1){
				countrypara = countrypara.replace(" ","-");
			}			
			while (citypara.indexOf(" ") > 1){
				citypara = citypara.replace(" ","-");
			}			
			var fullurl = rooturl + '/search-result-residential-lets/' + countrypara + '/' + citypara + '/' + bedroompara + '/' + minpricepara + '/' + maxpricepara;
			window.location = fullurl;
		}
		document.getElementById('res_filter_result').onclick=readPage;		
		document.getElementById('res_country').onchange=showcitylist;		
		showcitylist();
		

		function showcitylist(){
			var selectcity = document.getElementById('res_country').value;
			//alert(selectcity);
			var ajaxRequest;  // The variable that makes Ajax possible!
					try{
						// Opera 8.0+, Firefox, Safari
						ajaxRequest = new XMLHttpRequest();
					} catch (e){
						// Internet Explorer Browsers
						try{
							ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
						} catch (e) {
							try{
								ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
							} catch (e){
								// Something went wrong
								//alert("Your browser broke!");
								return false;
							}
						}
					}

					ajaxRequest.onreadystatechange = function(){
						if(ajaxRequest.readyState == 4){
							var ajaxDisplay                                 =  ajaxRequest.responseText;
							//document.getElementById( 'error' ).innerHTML  	=  ajaxDisplay;
							//alert(ajaxDisplay);
							document.getElementById('res_city').innerHTML = ajaxDisplay;	

						}
					}

				//var path	= document.getElementById('path').value;

				var url='http://geckodevelopment.co.uk/letyourlet/city-list-for-select-country/?country=' + selectcity + '&formid=2';
				ajaxRequest.open("GET", url, true);
				ajaxRequest.send(null);		
		}	
	});		
</script>	
	

<?php get_footer(); ?>